//
//  SZClassicLinesMainCell.m
//  KaiJing
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZClassicLinesMainCell.h"
#import <UIImageView+WebCache.h>

#define kXMargin 30
@implementation SZClassicLinesMainCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addAllSubViews];
    }
    return self;
}

/**
 *  添加所有的子视图
 */
- (void)addAllSubViews {
    // 添加底部ImageView
    _baseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth)];
    [self.contentView addSubview:_baseImageView];
    
    _introLabel = [[UILabel alloc] initWithFrame:CGRectMake(kXMargin, 0, kScreenWidth - kXMargin * 2, 200)];
    _introLabel.numberOfLines = 0;
    _introLabel.font = [UIFont systemFontOfSize:16];
    _introLabel.textColor = [UIColor whiteColor];
    [_baseImageView addSubview:_introLabel];
    
    _subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(kXMargin, 220, kScreenWidth - kXMargin * 2, 40)];
    _subjectLabel.textColor = [UIColor whiteColor];
    _subjectLabel.textAlignment = NSTextAlignmentRight;
    [_baseImageView addSubview:_subjectLabel];
    
    _bottomView = [[CJCellBottomView alloc] initWithFrame:CGRectMake(0, kScreenWidth - 35, kScreenWidth, 25)];
    [self.contentView addSubview:_bottomView];
    
    //点赞
    [_bottomView.praiseButton setImage:[UIImage imageNamed:@"parise_norml"] forState:(UIControlStateNormal)];
    [_bottomView.praiseButton setImage:[UIImage imageNamed:@"parise_select"] forState:(UIControlStateSelected)];
    _bottomView.praiseLabel.text = @"赞";
    _bottomView.praiseLabel.textColor = [UIColor whiteColor];
    //评论
    [_bottomView.commentButton setImage:[UIImage imageNamed:@"comment"] forState:(UIControlStateNormal)];
    _bottomView.commentLabel.text = @"评论";
    _bottomView.commentLabel.textColor = [UIColor whiteColor];
    
    //更多
    [_bottomView.moreButton setImage:[UIImage imageNamed:@"more"] forState:(UIControlStateNormal)];
    
    
    
}


/**
 *  赋值model
 *
 *  @param model 传入model
 */

- (void)setModel:(SZClassicLinesMainModel *)model {
    _model = model;
    NSString *urlString = [NSString stringWithFormat:@"http://morguo.com/%@",model.image];
    [_baseImageView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"recommend_selected"]];
    
    _introLabel.text = model.intro;
    _subjectLabel.text = [NSString stringWithFormat:@"-- %@",model.subject];
    
    // 自适应高度
    [self adjustViewsHeightWithContext:model.intro];
    
    if (model.isliked) {
        _bottomView.praiseButton.selected = YES;
        
        
    } else {
        _bottomView.praiseButton.selected = NO;
    }
    
    
}

/**
 *  自适应高度
 *
 *  @param context 传入的自适应高度文本
 */
- (void)adjustViewsHeightWithContext:(NSString *)context {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    CGRect rect = [context boundingRectWithSize:CGSizeMake(kScreenWidth - kXMargin * 2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    CGFloat totalHeght = rect.size.height + 80;
    _introLabel.top = (kScreenWidth - totalHeght) / 2;
    _introLabel.height = rect.size.height;
    
    _subjectLabel.top = _introLabel.bottom + 40;
}



@end
